﻿namespace AC.ExtendedRenderer.Toolkit
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(Button))]
    public class KryptonSplitButton : KryptonButton
    {
        private static readonly int BorderSize = (SystemInformation.Border3DSize.Width * 2);
        private Rectangle dropDownRectangle = new Rectangle();
        private const int PushButtonWidth = 14;
        private bool showSplit = true;
        private bool skipNextOpen;

        public KryptonSplitButton()
        {
            this.AutoSize = true;
        }

        private void ContextMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            ContextMenuStrip strip = sender as ContextMenuStrip;
            if (strip != null)
            {
                strip.Closing -= new ToolStripDropDownClosingEventHandler(this.ContextMenuStrip_Closing);
            }
            if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked)
            {
                this.skipNextOpen = this.dropDownRectangle.Contains(base.PointToClient(Cursor.Position));
            }
        }

        public override Size GetPreferredSize(Size proposedSize)
        {
            Size preferredSize = base.GetPreferredSize(proposedSize);
            if ((this.showSplit && !string.IsNullOrEmpty(this.Text)) && ((TextRenderer.MeasureText(this.Text, this.Font).Width + 14) > preferredSize.Width))
            {
                return (preferredSize + new Size(14 + (BorderSize * 2), 0));
            }
            return preferredSize;
        }

        protected override bool IsInputKey(Keys keyData)
        {
            return ((keyData.Equals(Keys.Down) && this.showSplit) || base.IsInputKey(keyData));
        }

        private void KryptonContextMenu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            KryptonContextMenu menu = sender as KryptonContextMenu;
            if (menu != null)
            {
                menu.Closed -= new ToolStripDropDownClosedEventHandler(this.KryptonContextMenu_Closed);
            }
            this.skipNextOpen = this.dropDownRectangle.Contains(base.PointToClient(Cursor.Position));
        }

        protected override void OnKeyDown(KeyEventArgs kevent)
        {
            if (this.showSplit && kevent.KeyCode.Equals(Keys.Down))
            {
                this.ShowContextMenuStrip();
            }
            base.OnKeyDown(kevent);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!this.showSplit)
            {
                base.OnMouseDown(e);
            }
            else if (this.dropDownRectangle.Contains(e.Location))
            {
                this.ShowContextMenuStrip();
            }
            else
            {
                base.OnMouseDown(e);
            }
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            if (!this.showSplit)
            {
                base.OnMouseUp(mevent);
            }
            else if ((this.ContextMenuStrip == null) || !this.ContextMenuStrip.Visible)
            {
                base.OnMouseUp(mevent);
            }
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            if (this.showSplit)
            {
                Graphics g = pevent.Graphics;
                Rectangle clientRectangle = base.ClientRectangle;
                this.dropDownRectangle = new Rectangle((clientRectangle.Right - 14) - 1, BorderSize, 14, clientRectangle.Height - (BorderSize * 2));
                int borderSize = BorderSize;
                Rectangle rectangle2 = new Rectangle(borderSize, borderSize, (clientRectangle.Width - this.dropDownRectangle.Width) - borderSize, clientRectangle.Height - (borderSize * 2));
                IPalette currentGlobalPalette = KryptonManager.CurrentGlobalPalette;
                Pen buttonShadow = SystemPens.ButtonShadow;
                Pen buttonFace = SystemPens.ButtonFace;
                if (currentGlobalPalette != null)
                {
                    buttonShadow = new Pen(currentGlobalPalette.ColorTable.GripDark);
                    buttonFace = new Pen(currentGlobalPalette.ColorTable.GripLight);
                }
                if (this.RightToLeft == RightToLeft.Yes)
                {
                    this.dropDownRectangle.X = clientRectangle.Left + 1;
                    rectangle2.X = this.dropDownRectangle.Right;
                    g.DrawLine(buttonShadow, clientRectangle.Left + 14, BorderSize, clientRectangle.Left + 14, clientRectangle.Bottom - BorderSize);
                    g.DrawLine(buttonFace, (clientRectangle.Left + 14) + 1, BorderSize, (clientRectangle.Left + 14) + 1, clientRectangle.Bottom - BorderSize);
                }
                else
                {
                    g.DrawLine(buttonShadow, clientRectangle.Right - 14, BorderSize, clientRectangle.Right - 14, clientRectangle.Bottom - BorderSize);
                    g.DrawLine(buttonFace, (clientRectangle.Right - 14) - 1, BorderSize, (clientRectangle.Right - 14) - 1, clientRectangle.Bottom - BorderSize);
                }
                PaintArrow(g, this.dropDownRectangle);
            }
        }

        private static void PaintArrow(Graphics g, Rectangle dropDownRect)
        {
            Point point = new Point(Convert.ToInt32((int) (dropDownRect.Left + (dropDownRect.Width / 2))), Convert.ToInt32((int) (dropDownRect.Top + (dropDownRect.Height / 2))));
            point.X += dropDownRect.Width % 2;
            Point[] points = new Point[] { new Point(point.X - 2, point.Y - 1), new Point(point.X + 3, point.Y - 1), new Point(point.X, point.Y + 2) };
            g.FillPolygon(SystemBrushes.ControlText, points);
        }

        private void ShowContextMenuStrip()
        {
            if (this.skipNextOpen)
            {
                this.skipNextOpen = false;
            }
            else if (this.KryptonContextMenu != null)
            {
                this.KryptonContextMenu.Show(base.FindForm().PointToScreen(base.Location) + new Size(0, base.Height));
                this.KryptonContextMenu.Closed += new ToolStripDropDownClosedEventHandler(this.KryptonContextMenu_Closed);
            }
            else if (this.ContextMenuStrip != null)
            {
                this.ContextMenuStrip.Closing += new ToolStripDropDownClosingEventHandler(this.ContextMenuStrip_Closing);
                this.ContextMenuStrip.Show(this, new Point(0, base.Height), ToolStripDropDownDirection.BelowRight);
            }
        }

        [DefaultValue(true)]
        public bool ShowSplit
        {
            get
            {
                return this.showSplit;
            }
            set
            {
                if (value != this.showSplit)
                {
                    this.showSplit = value;
                    base.Invalidate();
                    if (base.Parent != null)
                    {
                        base.Parent.PerformLayout();
                    }
                }
            }
        }
    }
}

